package cn.gwm.flink.streaming.function.map;

import cn.gwm.flink.streaming.beans.termalRunaway.WaringTermalDwmWithStragegyEv;
import cn.gwm.flink.streaming.constant.DefaultConstant;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.flink.api.common.functions.MapFunction;

import java.util.Objects;

/**
 * @author GW00283474
 */
public class TermalWaringNullEvMapFunction implements MapFunction<WaringTermalDwmWithStragegyEv, WaringTermalDwmWithStragegyEv> {
    @Override
    public WaringTermalDwmWithStragegyEv map(WaringTermalDwmWithStragegyEv value) throws Exception {
        if(Objects.isNull(value.getModelname())){
            value.setModelname(DefaultConstant.model_name);
        }
        if(Objects.isNull(value.getLatitude())){
            value.setLatitude(DefaultConstant.latitude);
        }
        if(Objects.isNull(value.getLongitude())){
            value.setLongitude(DefaultConstant.longitude);
        }
        if(Objects.isNull(value.getBMS_BattHeatRunaway())){
            //0 没发生电池热失控  1 发生电池热失控 缺省值为0
            value.setBMS_BattHeatRunaway(DefaultConstant.BMS_BattHeatRunaway);
        }

        if(Objects.isNull(value.getBMS_RMC_ModuleTempMax())){
            //缺省值
            value.setBMS_RMC_ModuleTempMax(DefaultConstant.BMS_RMC_ModuleTemp);
        }

        if(Objects.isNull(value.getBMS_RMC_ModuleTempMin())){
            //缺省值
            value.setBMS_RMC_ModuleTempMin(DefaultConstant.BMS_RMC_ModuleTemp);
        }

        if(Objects.isNull(value.getBMS_RMC_CellVoltMax())){
            //缺省值
            value.setBMS_RMC_CellVoltMax(DefaultConstant.BMS_SingleCellVolt);
        }
        if(Objects.isNull(value.getBMS_RMC_CellVoltMin())){
            //缺省值
            value.setBMS_RMC_CellVoltMin(DefaultConstant.BMS_SingleCellVolt);
        }
        if(Objects.isNull(value.getBMS_SOC())){
            //缺省值
            value.setBMS_SOC(DefaultConstant.BMS_SOC);
        }

        //填充电池模组默认缺省值
        value=fillModule(value);
        //填充单体电压默认缺省值
        value=fillVolt(value);
        return value;
    }


    /**
     * 填充单体电压默认缺省值
     * @param value
     * @return
     */
     WaringTermalDwmWithStragegyEv fillVolt(WaringTermalDwmWithStragegyEv value){
        JSONObject obj = JSONUtil.parseObj(value);
        obj.entrySet().forEach(entry->{
            if(entry.getKey().startsWith(DefaultConstant.BMS_SINGLE_CELL_EV_PREFIX)){
                String key=entry.getKey();
                obj.putOpt(key,obj.getObj(key,DefaultConstant.BMS_SingleCellVolt));
            }
        });
        return JSONUtil.toBean(obj, WaringTermalDwmWithStragegyEv.class);
    }
    /**
     * 填充电池模组电压默认缺省值
     * @param value
     * @return
     */
    WaringTermalDwmWithStragegyEv fillModule(WaringTermalDwmWithStragegyEv value){
        JSONObject obj = JSONUtil.parseObj(value);
        obj.entrySet().forEach(entry->{
            if(entry.getKey().startsWith(DefaultConstant.BMS_MODULE_TEMP_EV_PREFIX)){
                String key= entry.getKey();
                obj.putOpt(key,obj.getObj(key,DefaultConstant.BMS_BattModuleTemp));
            }
        });
        return JSONUtil.toBean(obj, WaringTermalDwmWithStragegyEv.class);
    }


}
